﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class AbstractTower : MonoBehaviour {
    public Transform influenceCenter;
    public float influenceRange;
    public int influencePoint;
    public int MaxInflucePoint;
    public PlayerType TowerOwner;

    public void UpdateinfluencePoint()
    {
        var surroundUnitCount = new Dictionary<PlayerType, int>();
        Collider[] colliders = Physics.OverlapSphere(influenceCenter.position , influenceRange, LayerMask.GetMask(LayerNameSet.Unit));
        foreach (var c in colliders)
        {
            var warrior = c.GetComponent<Warrior>();
            if (warrior != null)
            {
                if(surroundUnitCount.ContainsKey(warrior.unitOwner))
                {
                    surroundUnitCount[warrior.unitOwner]++;
                }
                else
                {
                    surroundUnitCount.Add(warrior.unitOwner, 0);
                }
            }
        }
        KeyValuePair<PlayerType, int> MaxUnitType = new KeyValuePair<PlayerType, int>(PlayerType.None,0);
        foreach (var v in surroundUnitCount)
        {
            if(MaxUnitType .Key== PlayerType.None || v.Value>MaxUnitType.Value)
            {
                MaxUnitType = v;
            }
        }
        if (MaxUnitType.Key == PlayerType.None) return;
        if(MaxUnitType.Key== TowerOwner)
        {
            if(influencePoint<MaxInflucePoint)
            {
                influencePoint++;
            }

        }
        else
        {
            if(influencePoint>0)
            {
                influencePoint--;
            }
            else
            {
                TowerOwner = MaxUnitType.Key;
            }
        }

    }
    

}
